/*
============================================================================
 Name        : RssReader.h
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : CRssReader declaration
============================================================================
*/

#ifndef __RSSREADER_H__
#define __RSSREADER_H__

// INCLUDES
#include <e32std.h>
#include <e32base.h>

#include "Connection.h"			//MConnectionObserver
#include "MXmlObserver.h"		//MXmlObserver

class CXmlEngine;
class TRssFeed;
class TRssFeedItem;

class MReaderObserver
{
public:
	virtual void DocumentEnd() = 0;
	virtual void RunError(const TDesC& aErrorDesc, TInt aError) = 0;
};

// CLASS DECLARATION
class CRssReader : public CActive, public MConnectionObserver, public MXmlObserver
{
public: // Constructors and destructor
	~CRssReader();
	static CRssReader* NewL(const TDesC& aUri, MReaderObserver& aObserver);
	static CRssReader* NewLC(const TDesC& aUri, MReaderObserver& aObserver);

private:
	CRssReader(MReaderObserver& aObserver);
	void ConstructL(const TDesC& aUri);

private: //from CActive
	void RunL();
	void DoCancel();
	//TInt RunError(TInt aError);

private: //from MConnectionObserver
	void ResponseRequestedL(TDesC8& aResponse);
	void StreamingRequestedL(TDesC8& /*aChunk*/) {};
	void StreamingEndedL() {};
	void ResponseErrorL(TInt aError);


private: //from MXmlObserver
	void StartElementRead(const TDesC& aNodeName, const RArray<TXmlAttribute> aAttributes);
	void EndElementRead(const TDesC& aNodeName, const TDesC& aData);


public: //new functions
	void StartL();
	TRssFeed* GetRssFeed();


private: //new functions
	void NotifyError(TInt aError);
	void GetRequestL();
	void CloseAndClean();
	void ParseXmlL(TDesC8& aResponse);


private: //data
	enum TState
	{
		ENotReady,
		EReadyToGetRequest,
		EReceivingData,
		EReadOk,
		EReadFailed
	} iState; //State of active object

	//Connection object
	CConnection* iConnection;
	
	//url to connect
	HBufC* iUri;

	//reader observer 
	MReaderObserver& iObserver;

	//rss feed result
	TRssFeed* iRssFeed;

	//rss current item read
	TRssFeedItem* iCurrentItem;	

	//Provides async timing service
	RTimer iTimer;

	//Error description
	TBuf<50> iError;

	//Timeout control
	TInt iLastActionTime;
};

#endif // __RSSREADER_H__
